package edu.upb.acs.jobsmaps.voice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;

import java.util.List;

/**
 * Created by dani on 6/28/2014.
 */
public class VoiceUtils {

	public static final int VOICE_RECOGNITION_REQUEST_CODE = 1001;

	public static Intent initializeIndent(Intent intent, Context context) {
		intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
//        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "voice.recognition.test");
		intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, context.getPackageName());
		//intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
		intent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, 5);
		return intent;
	}

	public static void speak(View view, List<String> valuesSpinner, Activity activity) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

		// Specify the calling package to identify your application
		intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity.getClass()
				.getPackage().getName());

		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);

		int noOfMatches = 5;
		intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, noOfMatches);

		if (null != valuesSpinner) {
			String extraDisplay = "Choose:";
			for (int i = 0; i < valuesSpinner.size(); i++) {
				extraDisplay += valuesSpinner.get(i) + "/";
			}
			Log.d("VoiceUtils", "Add extra tag = " + extraDisplay);
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
					extraDisplay);
		} else {
			intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
					"Please input voice command");
		}

		//Start the Voice recognizer activity for the result.
		activity.startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
	}

}
